﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

using SsrsRenderStudio.Renderer.Support;

namespace SsrsRenderStudio.Renderer.Output.Version2010
{
    public partial class FormOutputForm : Form
    {
        private int HEIGHT_ADDER = -20;
        private int WIDTH_ADDER = 0;

        System.Drawing.Imaging.Metafile _metafile;
        private Point[] _destinationPoints;

        /*
        float _ScreenDpiX = 120f;
        float _ScreenDpiY = 120f;
        float _mfDpiX = 120f;
        float _mfDpiY = 120f;
        */

        public FormOutputForm(System.Drawing.Imaging.Metafile metafile)
        {
            /*
            Graphics g = this.CreateGraphics();
            _ScreenDpiX = g.DpiX;
            _ScreenDpiY = g.DpiY;

            MetafileHeader mfH = _metafile.GetMetafileHeader();
            _mfDpiX = mfH.DpiX;
            _mfDpiY = mfH.DpiY;
            */

            InitializeComponent();
            _metafile = metafile;
            UpdateDestinationPoints();

            this.Paint += new PaintEventHandler(FormOutputForm_Paint);
            this.Resize += new EventHandler(FormOutputForm_Resize);
            this.ResizeRedraw = true;
            
        }

        void FormOutputForm_Resize(object sender, EventArgs e)
        {
            UpdateDestinationPoints();
        }

        void UpdateDestinationPoints()
        {
            _destinationPoints = new Point[3] { new Point(0, 0),
                                                new Point(Convert.ToInt16(this.Width) + WIDTH_ADDER, 0), 
                                                new Point(0, Convert.ToInt16(this.Height) + HEIGHT_ADDER) };
        }

        //private bool firstPaintCall = true;

        void FormOutputForm_Paint(object sender, PaintEventArgs e)
        {
            //e.Graphics.ScaleTransform(_mfDpiX / _ScreenDpiX, _mfDpiY / _ScreenDpiY, System.Drawing.Drawing2D.MatrixOrder.Prepend);
            //e.Graphics.DrawImageUnscaled(_metafile, new Point(0,0));

            //e.Graphics.DrawImage(_metafile, 0, 0);

            //e.Graphics.EnumerateMetafile(_metafile, _destinationPoints, _delegate);

            /*
            if (!firstPaintCall)
            {
                Renderer.MetafilePainter.PaintImage(e.Graphics, null, _destinationPoints);
            }
            else
            {
                Renderer.MetafilePainter.PaintImage(e.Graphics, _metafile, _destinationPoints);
                firstPaintCall = false;
            }
            */

            
            //e.Graphics.RotateTransform(90);
            //e.Graphics.TranslateTransform(0,-400);
            //e.Graphics.DrawString("O", new Font(FontFamily.GenericSerif, 10), new SolidBrush(Color.Black), new PointF(0, 0));
            //e.Graphics.TranslateTransform(100, 100);
            //e.Graphics.DrawLine(new Pen(Color.Black), 0, 0, 100, 100);
            //e.Graphics.DrawString("O", new Font(FontFamily.GenericSerif, 10), new SolidBrush(Color.Black), new PointF(0, 0));

            MetafilePainterInstance painter = new MetafilePainterInstance();
            painter.PaintImage(e.Graphics, _metafile, _destinationPoints);
        }

        
    }
}
